﻿
#pragma once
#ifndef __CX_ALL_TYPE__
#define	__CX_ALL_TYPE__

#include <iostream>
using std::cout;
using std::endl;

#ifdef WIN32
	#ifndef		_WIN32_WINNT
		#define		_WIN32_WINNT			0x0701
	#endif
#endif

typedef			unsigned short		WORD;
typedef _W64	unsigned long		ULONG_PTR, *PULONG_PTR;
typedef			unsigned long		DWORD;
typedef			ULONG_PTR			DWORD_PTR;

typedef			unsigned int	UINT	;
typedef			unsigned long	ULONG	;
typedef			unsigned char	UCHAR	;
typedef			UCHAR			BYTE	;


#if	defined(_DEBUG) || defined(NDEBUG)

	#define		DEBUG_PRINT			std::cerr
	#define		PRINT_END			std::endl

	#include <assert.h>

#if	defined(_WIN32)

	#define		CX_ASSERT(expr)					assert(expr)

	#ifdef _UNICODE
			#define		CX_ASSERT_MSG(expr,msg)		\
				_wassert(_CRT_WIDE(msg##L"\n"), _CRT_WIDE(__FILE__), __LINE__)
	#else 
			#define		CX_ASSERT_MSG(expr,msg)		\
				_wassert( msg##"\n", __FILE__, __LINE__)
	#endif

#elif defined(__unix__)
		#define		CX_ASSERT(expr)		assert(expr)
		#define		CX_ASSERT_MSG(expr,msg)		__assert(	msg		 , __FILE__, __LINE__)	
#endif

	#define		CX_S_ASSERT(expr)				static_assert(expr,#expr);
 
	#define		CX_S_ASSERT_MSG(expr,msg)		static_assert(expr,msg);

	#define		CX_ERR_MESSAGE(msg)		{\
									DEBUG_PRINT<<"Files:\n\t"<<__FILE__<<PRINT_END;\
									DEBUG_PRINT<<"\nLines:\n\t"<<__LINE__<<PRINT_END;\
									DEBUG_PRINT<<"\nMessage:\n\t"<<(msg)<<PRINT_END;}

#else 
	#define CX_ERR_MESSAGE(msg)
#endif


#define		BEGIN_NAMESPACE(space)		\
										namespace space	{
#define		END_NAMESPACE				\
										}


#define		CX_NO_IMP					CX_ASSERT_MSG(0,"Have no implement...")
#define     CX_SLOT                     protected


#ifndef MAKEWORD
	#define MAKEWORD(a, b)      ((WORD)(((BYTE)(((DWORD_PTR)(a)) & 0xff)) | ((WORD)((BYTE)(((DWORD_PTR)(b)) & 0xff))) << 8))
#endif
#ifndef MAKELONG
	#define MAKELONG(a, b)      ((LONG)(((WORD)(((DWORD_PTR)(a)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(b)) & 0xffff))) << 16))
#endif
#ifndef LOWORD
	#define LOWORD(l)           ((WORD)(((DWORD_PTR)(l)) & 0xffff))
#endif	
#ifndef HIWORD
	#define HIWORD(l)           ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
#endif	
#ifndef LOBYTE
	#define LOBYTE(w)           ((BYTE)(((DWORD_PTR)(w)) & 0xff))
#endif
#ifndef HIBYTE
	#define HIBYTE(w)           ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff))
#endif

#if	!defined(USING_BOOST)
		#include <exception>
		typedef		std::error_code				ERRCODE;
#else
	typedef		boost::system::error_code		ERRCODE ;
#endif

inline	void   	__EmptyFunc(bool){}

#if	defined(USING_BOOST)
	typedef		boost::function<void(const ERRCODE&,size_t )>	 Async_Handler;
#else 
	typedef		void			(*Async_Handler)(const ERRCODE&,size_t);
#endif


namespace locker{
	#if defined(QT_CORE_LIB)
	#include <QMutex>
		typedef		QMutex						_Mutex;
		typedef		QMutexLocker				_Locker;
		#define		__Lock(mu)					_Locker	locker(&mu)

	#elif defined(_WIN32)
	#include <Windows.h>
	class		CxMutex
	{
	public:
		CxMutex()		{
			InitializeCriticalSection(&cs);
		}
		~CxMutex()		{DeleteCriticalSection(&cs);}
		void	Lock()	{EnterCriticalSection(&cs);}
		void	unLock(){LeaveCriticalSection(&cs);}
	private:
		CRITICAL_SECTION			cs;
	};
	class		CxLocker
	{
	public:
		CxLocker(CxMutex *_mu):mu(_mu){
			if(_mu!=NULL)
				_mu->Lock();
		}
		~CxLocker(){
			if(mu!=NULL)
				mu->unLock();
		}
	private:
		CxMutex			*mu;
	};
		typedef		CxMutex							_Mutex;
		typedef		CxLocker						_Locker;
		#define		__Lock(mu)		_Locker			locker(&mu)


	#elif	defined(__unix__)
	#include <pthread.h>
	
	class		CxMutex
	{
	public:
		CxMutex()		{
			pthread_mutex_init(&ps,NULL);
		}
		~CxMutex()		{
			pthread_mutex_destory(&ps);
		}
		void	Lock()	{pthread_mutex_lock(&ps);}
		void	unLock(){pthread_mutex_unlock(&cs);}
	private:
		pthread_mutex_t				ps;
	};

	class		CxLocker
	{
	public:
		CxLocker(CxMutex *_mu):mu(_mu){
			if(_mu!=NULL)
				_mu->Lock();
		}
		~CxLocker(){
			if(mu!=NULL)
				mu->unLock();
		}
	private:
		CxMutex			*mu;
	};
		typedef		CxMutex							_Mutex;
		typedef		CxLocker						_Locker;
		#define		__Lock(mu)		_Locker			locker(&mu)


	#elif defined(USING_BOOST)
	#include <boost\thread.hpp>
		typedef		boost::mutex				_Mutex;
		typedef		boost::mutex::scoped_lock	_Locker;
		#define		__Lock(mu)					_Locker	locker(mu)
	#endif
}


#endif